package labox.innovation.gameserver.templates.item;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.config.Config;
import labox.innovation.gameserver.model.L2ItemInstance;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;
import labox.innovation.gameserver.skills.Stats;
import labox.innovation.gameserver.skills.funcs.AttributeStringSplice;
import labox.innovation.gameserver.templates.item.FuzeEconomicManager.OPSTATE;
import labox.innovation.gameserver.templates.item.ItemIdentifyConfig.ItemIdentifyData;
import labox.innovation.gameserver.templates.item.ItemIdentifyConfig.ItemNums;
import labox.innovation.util.Rnd;

/**
 * ��Ʒ ��ϵͳ ���ϵͳ
 * 
 * @author zjj
 * 
 */
public class ItemIdentifyManage {

	private static final Logger _log = LoggerFactory.getLogger(ItemIdentifyManage.class.getName());

	public static final int IDENTIFY_ID = 0; // ��
	public static final int INSCRIBE_ID = 1; // ���

	public static final ItemIdentifyManage getInstance() {
		return SingletonHolder._instance;
	}

	/**
	 * ���ɫ�� ��Ʒ
	 * 
	 * @param pc
	 * @param item
	 * @param flagId
	 *            : �� 1 �� ��� 2
	 * @return
	 */
	public boolean identify(final FuzePcInstance pc, L2ItemInstance item, int flagId) {
		// �����Ʒ�Ѿ����� ��ô�����ڱ���
		// �����Ʒ�Ѿ�����̣� ��ô�����ڱ����
		// ������Ҫ������Ϣ��ͻ���
		if (((flagId == IDENTIFY_ID) && item.isIdentify()) || ((flagId == INSCRIBE_ID) && item.isInscribe())) {
			// System.out.println("�Ѽ� ��  ���");
			return false;
		}

		ItemIdentifyData itemCfgData = null;

		// ��ô� ��Ʒ ��� �� �� ���������
		if (flagId == IDENTIFY_ID) {
			itemCfgData = ItemIdentifyConfig.getInstance().getItemConfigData(item.getItemLvl());
		} else {
			itemCfgData = ItemInscribeConfig.getInstance().getItemConfigData(item.getItemLvl());
		}

		if (itemCfgData == null) {
			_log.warn("�����ļ�������", Config.SERVER_ID);
			return false;
		}

		List<ItemNums> itemnum = itemCfgData.getAllConsumeItems();

		// ��� ��Ʒ�����Ƿ�����
		for (ItemNums temp : itemnum) {
			// ���� �������⴦�� Ǯ�����⡣ ��ȡ��ɫ���ϵ�Ǯ ����ֱ�ӻ�ã� ����Ҫȥ ������ȥ����
			L2ItemInstance needItem = pc.getInventory().getItemByItemId(temp.itemId);
			// ���û�� ����� ��ô���ܼ�
			if (needItem == null || needItem.getCount() < temp.itemNums) {
				SystemMessage sm = new SystemMessage(SystemMessageId.YOU_NOTHAVE_ITEM_S1);
				sm.addItemName(temp.itemId);
				pc.sendPacket(sm);
				return false;
			}

		}// end for( ItemNums temp:itemnum )

		// ��ɫ�����Ƿ����㹻��Ǯ
		// if( !pc.getInventory().hasEnoughSilverMoney( OPSTATE.FORGE.getValue()
		// , pc , itemCfgData._moneySilverNums ) )
		// {
		// // System.out.println("���Ĳ���");
		// pc.sendPacket( new SystemMessage(
		// SystemMessageId.YOU_NOT_ENOUGH_ADENA ));
		// return false;
		// }

		if (pc.getMoneyNote() < itemCfgData._moneySilverNums) {
			pc.sendPacket(new SystemMessage(SystemMessageId.NOT_ENOUGH_MONEY_NOTE));
			return false;
		}

		// ���ٱ����еĵ���
		for (ItemNums temp : itemnum) {
			pc.destroyItemByItemId("", temp.itemId, temp.itemNums, null, true);
		}// end

		if (itemCfgData._moneyType == FuzeEconomicManager.MONEY_TYPE_SILVER) {
			// �۳��ɫ���ϵ�������
			pc.reduceMoneySilver(OPSTATE.FORGE.getValue(), itemCfgData._moneySilverNums, null, true);
		} else if (itemCfgData._moneyType == FuzeEconomicManager.MONEY_TYPE_NOTE) {
			pc.reduceMoneyNote(null, itemCfgData._moneySilverNums, null, true);
		} else if (itemCfgData._moneyType == FuzeEconomicManager.MONEY_TYPE_YUANBAO) {
			pc.reduceMoneyYuanBao(null, itemCfgData._moneySilverNums, null, true);
		}

		// pc.getInventory().reduceMoneySilver( OPSTATE.FORGE.getValue(),
		// itemCfgData._moneySilverNums, pc, null);

		// ��Ʒ���� �� �� ��� ����
		item.addItemBouns(new IdentifyBouns(item.getItem()._attributs, itemCfgData, flagId));
		item.reduceEndurance((int) (item.getEnduranceUp() * L2ItemInstance.REDUCEENRU_NUMS));

		if (flagId == IDENTIFY_ID) {

			_log.info("������Ʒ: ��ɫ:" + pc.getName() + "(" + pc.getObjectId() + ")" + "����Ʒ:" + item.getName() + "(" + item.getObjectId() + ")" + "��", pc.getObjectId(), pc.getName());

		} else {
			_log.info("������Ʒ: ��ɫ:" + pc.getName() + "(" + pc.getObjectId() + ")" + "����Ʒ:" + item.getName() + "(" + item.getObjectId() + ")" + "���", pc.getObjectId(), pc.getName());
		}

		return true;

	}// end identify

	/**
	 * ���� �� �� ��̵�����
	 * 
	 * @param item
	 * @param factor
	 *            : ϵ��
	 * @param flagid
	 *            : �� �� ���
	 */
	public void identify(L2ItemInstance item, int factor, int flagid) {
		if (item == null) {
			return;
		}

		item.addItemBouns(new IdentifyBouns(item.getItem()._attributs, factor, flagid));

	}

	/**
	 * �� or ��� ���Խ���
	 * 
	 * @author zjj
	 * 
	 */
	public static class IdentifyBouns extends ItemBouns {

		private static final int RATE = 100;

		protected String _attributeStr; // �����ַ�����

		protected int _flagid; // ��̻� �� ����ֶ�

		protected int _bounsrate = -1; // ��� �� ��ϵ��

		public int getFlagId() {
			return _flagid;
		}

		public int getBounsRate() {
			return _bounsrate;
		}

		// attribute : ģ�����ó��� ������
		public IdentifyBouns(final Map<Integer, Integer> attribute, final ItemIdentifyData fcgData, int flagid) {
			if (flagid == IDENTIFY_ID) {
				_itemBounsType = ItemBounsType.IDENTIFY_BOUNS; // ��
			} else {
				_itemBounsType = ItemBounsType.INSCRIBE_BOUNS; // ���
			}

			_flagid = flagid;
			makeBouns(attribute, fcgData, flagid);

		}

		/**
		 * Ϊ��ֵ��ʹ��
		 * 
		 * @param factor
		 *            : ϵ��
		 * @param flagid
		 *            : �� �� ��ǩ
		 */
		public IdentifyBouns(final Map<Integer, Integer> attribute, int factor, int flagid) {

			if (flagid == IDENTIFY_ID) {
				_itemBounsType = ItemBounsType.IDENTIFY_BOUNS; // ��
			} else {
				_itemBounsType = ItemBounsType.INSCRIBE_BOUNS; // ���
			}

			_flagid = flagid;
			makeBouns(attribute, factor);

		}

		/**
		 * ����Ʒ ���õĽ���
		 * 
		 * @param attribute
		 *            : ģ�����ó��� ������
		 */
		protected void makeBouns(final Map<Integer, Integer> attribute, final ItemIdentifyData fcgData, int flagid) {
			if (flagid == IDENTIFY_ID) {
				_bounsrate = Rnd.get(fcgData._bounsMin, fcgData._bounsMax);
			} else {
				_bounsrate = fcgData._bounsMin;
			}

			if (_log.isDebugEnabled()) {
				_log.debug("���Խ���ٷֱ�:" + _bounsrate);
			}

			// Iterator<Map.Entry<Integer, Integer>> it =
			// attribute.entrySet().iterator();
			//			
			// while (it.hasNext()) {
			// Map.Entry<Integer, Integer> mapEn = it.next();
			// int key = mapEn.getKey();
			// int value = mapEn.getValue();
			//				
			// float newVl = value;
			// newVl *= (newVl * _bounsrate) / RATE;
			// value = (int)Math.ceil( newVl );
			//				
			// addFuncs( Stats.values()[key], value );
			// addAttribute( key, value );
			//				
			// }// end while (it.hasNext())
			makeBouns(attribute, _bounsrate);

		}// end makeBouns

		/**
		 * �� �� �����Ʒ ���õĽ���
		 * 
		 * @param attribute
		 * @param factor
		 *            : ϵ��
		 */
		protected void makeBouns(final Map<Integer, Integer> attribute, int factor) {
			_bounsrate = factor;
			// System.out.println("ϵ��:" + _bounsrate );
			Iterator<Map.Entry<Integer, Integer>> it = attribute.entrySet().iterator();

			while (it.hasNext()) {
				Map.Entry<Integer, Integer> mapEn = it.next();
				int key = mapEn.getKey();
				int value = mapEn.getValue();

				float newVl = value;

				if (_log.isDebugEnabled()) {
					_log.debug(Stats.values()[key] + "��ʼֵ:" + newVl);
				}

				newVl = (newVl * _bounsrate) / RATE;
				// System.out.println("ϵ��:" + _bounsrate);
				// System.out.println("����:" + RATE);
				// System.out.println("ֵ:" + newVl);
				value = (int) Math.ceil(newVl);
				if (value == 0) {
					value = 1;
				}

				addFuncs(Stats.values()[key], value);
				addAttribute(key, value);

			}// end while (it.hasNext())

		}

		@Override
		public String getAttributeDescribe() {
			// TODO Auto-generated method stub
			if (_attributeStr == null) {
				if (_flagid == IDENTIFY_ID) {
					_attributeStr = "<a>������:</a><br/>";
				} else {
					_attributeStr = "<a>�������:</a><br/>";
				}
				_attributeStr += AttributeStringSplice.getInstance().getAttributeString(_attributes);
			}

			return _attributeStr;
		}

	}// end class IdentifyBouns

	@SuppressWarnings("synthetic-access")
	private static class SingletonHolder {
		protected static final ItemIdentifyManage _instance = new ItemIdentifyManage();
	}

	private ItemIdentifyManage() {
	}

}
